package com.ruike.alisurface.ui.mainPage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruike.alisurface.BuildConfig;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.http.MchApiHttp;
import com.ruike.alisurface.ui.setting.SettingActivity;
import com.ruike.alisurface.utils.DeviceUtils;
import com.ruike.alisurface.utils.ZXingUtils;
import com.tencent.rtmp.TXVodPlayer;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.voodoo.lib_utils.AssetsUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.imageLoader.ImageLoader;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: voodoo
 * CreateDate: 2020-03-29 029 下午 09:49
 * Description: 登录界面
 */
public class LoginActivity extends BaseAty {

    @BindView(R.id.login_video_txcvv)
    TXCloudVideoView loginVideoTxcvv;
    @BindView(R.id.login_versionName_tv)
    TextView loginVersionNameTv;
    @BindView(R.id.login_imgcode)
    ImageView mCodeImg;
    @BindView(R.id.login_sn_tv)
    TextView loginSnTv;
    @BindView(R.id.login_name_et)
    EditText loginNameEt;
    @BindView(R.id.login_pwd_et)
    EditText loginPwdEt;

    private TXVodPlayer mVodPlayer;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initViews() {
        L.i("登录页==" + this.getClass().getName());
        startCountdownTimer(60, null, null);
    }

    @Override
    protected void initData() {
        loginVersionNameTv.setText(new StringBuffer(getResources().getString(R.string.versionNameStr)).append(BuildConfig.VERSION_NAME));
        loginSnTv.setText(new StringBuffer(getResources().getString(R.string.deviceSnStr)).append(DeviceUtils.getDeviceSn()));
        playVideo();
        String jsonSN = "http://m.shouhuojiyun.com/android_login/index.html?sn=" + DeviceUtils.getDeviceSn();
        Bitmap SnBitmap = ZXingUtils.getInstance().createQRImage((jsonSN), 300, 300, BitmapFactory.decodeResource(getResources(), 0));
        assert SnBitmap != null;
        ImageLoader.loadImage(this, SnBitmap, mCodeImg);
    }

    /**
     * player对象创建
     */
    private void playVideo() {
        // 创建  对象
        mVodPlayer = new TXVodPlayer(this);
        //关键 player 对象与界面 view
        mVodPlayer.setPlayerView(loginVideoTxcvv);
        File file = AssetsUtils.getFile(this, "video_sunny.mp4");
        if (file != null && file.exists()) {
            mVodPlayer.startPlay(file.getAbsolutePath());
            mVodPlayer.setLoop(true);
        }
    }

    @OnClick({R.id.login_goback_imgv, R.id.login_tv})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.login_goback_imgv:
                startCountdownTimer(0, null, null);
                break;
            case R.id.login_tv:
                login();
                break;
        }
    }

    public void login() {
        String username = loginNameEt.getText().toString();
        String pwd = loginPwdEt.getText().toString();
        if (TextUtils.isEmpty(username)) {
            showErrorTip("账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            showErrorTip("密码不能为空");
            return;
        }
        showProgressDialog("登录中，请等待...");
        MchApiHttp.getLogin(this, username, pwd);
    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        super.onSuccess(requestUrl, requestJsonStr);
        if (requestUrl.contains("LoginByMch")) {
            L.i("登录页=onSuccess=" + requestJsonStr);
            startActivity(SettingActivity.class, null);
            finish();
        }

    }

    @Override
    public void onError(String requestUrl, String errorMsg, String data) {
        super.onError(requestUrl, errorMsg, data);
        if (requestUrl.contains("LoginByMch")) {
            L.i("登录页=onError=" + errorMsg);
            showErrorTip(errorMsg);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 暂停播放
        if (mVodPlayer != null) {
            mVodPlayer.pause();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        // 恢复播放
        if (mVodPlayer != null) {
            mVodPlayer.resume();
        }
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {
        if (event.getType().equals("login") && event.getMessage().equals("跳转后台")) {
            startActivity(SettingActivity.class, null);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVodPlayer.stopPlay(true); // true 代表清除最后一帧画面
        loginVideoTxcvv.onDestroy();
        loginVideoTxcvv = null;
    }
}
